package com.lty.zyq.service;


import com.lty.zyq.dao.MealOrderDao;
import com.lty.zyq.dao.UserDao;
import com.lty.zyq.entity.MealOrder;
import com.lty.zyq.entity.User;
import com.lty.zyq.entity.jsonBean.*;
import com.lty.zyq.entity.key.MealOrderKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MealOrderService {

    @Autowired
    MealOrderDao mealOrderDao;

    @Autowired
    TimeService timeService;

    @Autowired
    UserDao userDao;

    public OrderStateBean saveOrder(UserOrderBean userOrderBean) {
        MealOrder mealOrder = convertUserOrderToMealOrder(userOrderBean);
        mealOrderDao.saveAndFlush(mealOrder);

        return getOrderState(mealOrder.getMealOrderKey().getWhichMeal(), mealOrder.getMealOrderKey().getUserId());
    }

    private MealOrder convertUserOrderToMealOrder(UserOrderBean userOrderBean) {
        MealOrder mealOrder = new MealOrder();

        MealOrderKey mealOrderKey = new MealOrderKey(
                timeService.getTodayDateAndWeek().getDate(),
                userOrderBean.getWhichMeal(),
                userOrderBean.getId()
        );

        mealOrder.setMealOrderKey(mealOrderKey);
        mealOrder.setState(userOrderBean.getState());

        return mealOrder;
    }

    public OrderStateBean getOrderState(Integer whichMeal, Long userId) {

        MealOrderKey mealOrderKey = new MealOrderKey(
                timeService.getTodayDateAndWeek().getDate(),
                whichMeal,
                userId
        );

        MealOrder mealOrder = mealOrderDao.findByMealOrderKey(mealOrderKey);


        OrderStateBean orderStateBean = new OrderStateBean();
        List<String> departmentOrderNames = getDepartmentOrderNames(whichMeal, userId, timeService.getTodayDateAndWeek().getDate());
        orderStateBean.setFinalOrderNum(departmentOrderNames.size());
        orderStateBean.setOrderNames(departmentOrderNames);
        orderStateBean.setWholeOrderNum(getWholeOrderNum(whichMeal));

        //用户没有当前订单
        if (mealOrder == null) {
            orderStateBean.setState(0);
            orderStateBean.setWhichMeal(whichMeal);
        } else {
            orderStateBean.setState(mealOrder.getState());
            orderStateBean.setWhichMeal(mealOrder.getMealOrderKey().getWhichMeal());
        }


        return orderStateBean;

    }

    private List<String> getDepartmentOrderNames(Integer whichMeal, Long userId, String date) {

        List<User> userList = getDepartmentUsers(userId);

        List<MealOrderKey> mealOrderKeyList = new ArrayList<>();

        userList.forEach(user1 -> mealOrderKeyList.add(new MealOrderKey(date, whichMeal, user1.getId())));

        List<MealOrder> mealOrderList = mealOrderDao.findByMealOrderKeyInAndState(mealOrderKeyList, 1);

        List<Long> departmentOrderUserIdList = new ArrayList<>();
        mealOrderList.forEach(mealOrder -> departmentOrderUserIdList.add(mealOrder.getMealOrderKey().getUserId()));

        List<User> users = userDao.findByIdIn(departmentOrderUserIdList);

        List<String> departmentOrderUserNameList = new ArrayList<>();

        users.forEach(user1 -> departmentOrderUserNameList.add(user1.getName()));

        return departmentOrderUserNameList;

    }

    private List<User> getDepartmentUsers(Long userId) {
        User user = userDao.getById(userId);

        return userDao.findByDepartment(user.getDepartment());
    }

    private Integer getWholeOrderNum(Integer whichMeal) {
        return mealOrderDao.countByMealOrderKeyDateAndMealOrderKeyWhichMealAndState(timeService.getTodayDateAndWeek().getDate(), whichMeal, 1);
    }


    public TodayOrdersStateBean getTodayOrderState(Long userId) {

        TodayOrdersStateBean todayOrdersStateBean = new TodayOrdersStateBean();
        todayOrdersStateBean.setDate(timeService.getTodayDateAndWeek().getDate());
        todayOrdersStateBean.setWeak(timeService.getTodayDateAndWeek().getWeek());
        todayOrdersStateBean.setRemainTime(timeService.getRemainTime());

        List<OrderStateBean> orderStateBeanList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            orderStateBeanList.add(getOrderState(i, userId));
        }
        todayOrdersStateBean.setOrderStateList(orderStateBeanList);

        return todayOrdersStateBean;
    }

    public List<DepartmentOrderState> getDepartmentState(Long userId, Integer whichMeal) {

        List<User> userList = getDepartmentUsers(userId);

        List<MealOrderKey> mealOrderKeyList = new ArrayList<>();
        userList.forEach(user1 -> mealOrderKeyList.add(new MealOrderKey(timeService.getTodayDateAndWeek().getDate(), whichMeal, user1.getId())));

        List<MealOrder> mealOrderList = mealOrderDao.findByMealOrderKeyIn(mealOrderKeyList);

        List<DepartmentOrderState> departmentOrderStateList = new ArrayList<>();

        userList.forEach(user -> {
            if (!user.getId().equals(userId)) {
                DepartmentOrderState departmentOrderState = new DepartmentOrderState();
                departmentOrderStateList.add(departmentOrderState);
                departmentOrderState.setName(user.getName());
                departmentOrderState.setId(user.getId());
                mealOrderList.forEach(mealOrder -> {
                    if (mealOrder.getMealOrderKey().getUserId().equals(user.getId())) {
                        departmentOrderState.setHelperId(mealOrder.getHelperId());
                        departmentOrderState.setState(mealOrder.getState());
                    }
                });
            }
        });

        return departmentOrderStateList;
    }

    public void helpOrders(Integer whichMeal, List<HelpOrderBean> helpOrderBeans) {

        helpOrderBeans.forEach(helpOrderBean -> saveHelpOrder(whichMeal, helpOrderBean));

    }

    public void saveHelpOrder(Integer whichMeal, HelpOrderBean helpOrderBean) {
        MealOrder mealOrder = convertHelpOrderToMealOrder(whichMeal, helpOrderBean);
        mealOrderDao.saveAndFlush(mealOrder);
    }

    private MealOrder convertHelpOrderToMealOrder(Integer whichMeal, HelpOrderBean helpOrderBean) {
        MealOrder mealOrder = new MealOrder();

        MealOrderKey mealOrderKey = new MealOrderKey(
                timeService.getTodayDateAndWeek().getDate(),
                whichMeal,
                helpOrderBean.getId()
        );

        mealOrder.setMealOrderKey(mealOrderKey);
        mealOrder.setState(helpOrderBean.getState());
        mealOrder.setHelperId(helpOrderBean.getHelperId());

        return mealOrder;
    }
}
